package model

import (
	"github.com/google/uuid"
	"gorm.io/gorm"
	"time"
)

type Enter struct {
	UUID          uuid.UUID `json:"uuid" gorm:"primaryKey" binding:"required"`
	Sid           uint      `json:"sid" binding:"required"`
	Cid           string    `json:"cid" binding:"required"`
	Name          string    `json:"name" binding:"required"`
	Carid         string    `json:"carid" binding:"required"`
	Edate         time.Time `json:"edate" gorm:"default:;" binding:"required"`
	Quantity      uint      `json:"quantity" binding:"required"`
	Quality       float64   `json:"quality" binding:"required"`
	RemainQuality float64
}

type EnterAPI struct {
	Sid           uint      `json:"sid"`
	Cid           string    `json:"cid"`
	Name          string    `json:"name"`
	Carid         string    `json:"carid"`
	Edate         time.Time `json:"edate"`
	Quantity      uint      `json:"quantity"`
	Quality       float64   `json:"quality"`
	RemainQuality float64   `json:"RemainQuality"`
}

// AfterCreate 钩子函数创建后修改剩余吨数
func (enter *Enter) AfterCreate(tx *gorm.DB) (err error) {
	err = tx.Model(enter).Update("remain_quality", enter.Quality).Error
	return
}
